package com.fy.service.imbusiness.controller;


import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fy.service.imbusiness.service.ValidateCodeService;
import com.xiaoleilu.hutool.captcha.CaptchaUtil;
import com.xiaoleilu.hutool.captcha.ICaptcha;

import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * 验证码
 *
 * @author zhshuo
 * @date 2021/11/29 16:02
 **/
@Slf4j
@Controller
@Deprecated
@AllArgsConstructor
// @Api(tags = "验证码")
@RequestMapping("/validate")
public class ValidateCodeController {

    ValidateCodeService validateCodeService;

    @SneakyThrows
    @PostMapping("/code")
    // @ApiOperation("获取验证码")
    public void validateCode(@ApiParam(
            name = "deviceId",
            type = "String",
            value = "机器码",
            required = true) @RequestBody String deviceId, HttpServletResponse response) {
        Assert.notNull(deviceId, "机器码不能为空");
        response.setContentType("image/jpeg");
        response.setHeader("Pragma", "No-cache");
        ICaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100, 50);
        lineCaptcha.createCode();
        validateCodeService.saveValidateCode(lineCaptcha.getCode(), deviceId);
        log.info("validate code {}", lineCaptcha.getCode());
        lineCaptcha.write(response.getOutputStream());
        response.getOutputStream().close();
    }

}
